<?php
class adminController extends accessController{
    public static function _right(){
        return array(
            array('安全设置',1),
            array('管理员添加','add,add_'),
            array('管理员列表','li'),
            array('管理员编辑','mod,mod_'),
            array('管理员删除','del'),
        );
    }
    public function index(){
        
    }
    public function add(){
		$group_service=load_service('group');
		$group_arr=$group_service->with_option();
		$group_data=$group_service->make_option_array($group_arr);
		
        include($this->viewer_dir.'admin/add.php');
    }
    public function add_(){
        $admin=load_service('admin');
		$group=load_service('group');
		
		$r=$admin->is_founder(0);
		$r2=$group->is_foundergroupid($_POST['group_id']);
		
		if(!$r && $r2){
			alert('管理员组选择错误！');
            back();
		}
		
        $r=$admin->add_($_POST);
        if($r[0]){
            alert('管理员添加成功！');
            jump('admin','add');
        }else{
            alert('管理员添加失败！'.$r[1]);
            back();
        }
    }
    public function mod(){
        $admin=load_service('admin');
        $id=intval($_GET['id']);
        $r=$admin->judge($id);
        if(!$r){
            alert('此账号不允许编辑或删除！');
            reload();
        }
        $data=$admin->mod($_GET['id']);
        if($data){
			$group_service=load_service('group');
			$group_arr=$group_service->with_option();
			$group_data=$group_service->make_option_array($group_arr);
			
            include($this->viewer_dir.'admin/mod.php');
        }
    }
    public function mod_(){
        $admin=load_service('admin');
		$group=load_service('group');
		
        $id=intval($_GET['id']);
        $r=$admin->judge($id);
        if(!$r){
            alert('此账号不允许编辑或删除！');
            reload();
        }
		
		$r=$admin->is_founder($id);
		$r2=$group->is_foundergroupid($_POST['group_id']);
		
		if(!$r && $r2){
			alert('管理员组选择错误！');
            back();
		}
		
        $r=$admin->mod_($_POST,$id);
        if($r[0]){
            alert('管理员编辑成功！');
            reload();
        }else{
            alert('管理员编辑失败！'.$r[1]);
            back();
        }
    }
    public function del(){
        $admin=load_service('admin');
        $id=intval($_GET['id']);
        $r=$admin->judge($id);
        if(!$r){
            alert('此账号不允许编辑或删除！');
            back();
        }
        $r=$admin->del_myself($id);
        if($r){
            alert('无法删除自己的账号！');
            back();
        }
        if($admin->del($_GET['id'])){
            alert('删除成功！');
            jump('admin','li');
        }else{
            alert('删除失败！');
            back();
        }
    }
    public function li(){
		
		$group_service=load_service('group');
		$group_arr=$group_service->with_option();
		$group_data=$group_service->make_option_array($group_arr);
		
        $admin=load_service('admin');
        $return=$admin->li();
        include($this->viewer_dir.'admin/list.php');
    }
}